<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>mongo | 个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="1.server.js12345678910111213141516171819202122232425262728293031323334353637383940414243444546const mongoose &#x3D; require(&amp;#x27;mongoose&amp;#x27;);mongoose.connect(&#96;mongodb:&#x2F;&#x2F;127.0.0.1:27017&#x2F;my&#96;,function(e">
<meta property="og:type" content="article">
<meta property="og:title" content="mongo">
<meta property="og:url" content="https://gitee.com/wieldy/bailment-blog/2022/03/29/mongo/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="1.server.js12345678910111213141516171819202122232425262728293031323334353637383940414243444546const mongoose &#x3D; require(&amp;#x27;mongoose&amp;#x27;);mongoose.connect(&#96;mongodb:&#x2F;&#x2F;127.0.0.1:27017&#x2F;my&#96;,function(e">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-03-29T14:12:00.000Z">
<meta property="article:modified_time" content="2023-04-23T12:11:43.651Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/bailment-blog/atom.xml" title="个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/bailment-blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/bailment-blog/css/style.css">

  
    
<link rel="stylesheet" href="/bailment-blog/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/bailment-blog/" id="logo">个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/bailment-blog/">Home</a>
        
          <a class="main-nav-link" href="/bailment-blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/bailment-blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/wieldy/bailment-blog"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-mongo" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/bailment-blog/2022/03/29/mongo/" class="article-date">
  <time class="dt-published" datetime="2022-03-29T14:12:00.000Z" itemprop="datePublished">2022-03-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      mongo
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <ul>
<li>1.server.js<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>);</span><br><span class="line"></span><br><span class="line">mongoose.<span class="title function_">connect</span>(<span class="string">`mongodb://127.0.0.1:27017/my`</span>,<span class="keyword">function</span>(<span class="params">err</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(err)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(err);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;数据库链接成功&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">UserModel</span> = <span class="built_in">require</span>(<span class="string">&#x27;./model/user&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> <span class="title class_">ArticleModel</span> = <span class="built_in">require</span>(<span class="string">&#x27;./model/user&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> users = []</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; <span class="number">10</span>; i++)&#123;</span><br><span class="line">    users.<span class="title function_">push</span>(&#123;<span class="attr">username</span>:<span class="string">&#x27;zf&#x27;</span>+i,<span class="attr">password</span>:<span class="string">&quot;123456&quot;</span>,<span class="attr">age</span>:i&#125;);</span><br><span class="line">&#125;</span><br><span class="line">(<span class="keyword">async</span> <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="comment">// let docs =  await UserModel.create(users); </span></span><br><span class="line">    <span class="comment">// console.log(docs)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// find 返回的是数组  findOne / findById</span></span><br><span class="line">    <span class="comment">// let user = await UserModel.findById(&#x27;6290d711a7b5fb00c518030b&#x27;)</span></span><br><span class="line">    <span class="comment">// console.log(user)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 分页查询 $gt $lt</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// let docs = await UserModel.find(&#123;age:&#123;$gt:3&#125;&#125;); // 原生的方法都支持</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 内部 skip = 3  limit=2   </span></span><br><span class="line">    <span class="comment">// 内置sort 》 skip 》 limit</span></span><br><span class="line">    <span class="comment">// let docs = await UserModel.find(&#123;&#125;).sort(&#123;age:-1&#125;).limit(2).skip(3)</span></span><br><span class="line">    <span class="comment">// console.log(docs)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// let users = await UserModel.find(&#123;&#125;);</span></span><br><span class="line">    <span class="comment">// console.log(users)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 关联查询  user  -&gt;  article</span></span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    mongoose.<span class="title function_">disconnect</span>(); <span class="comment">// 关闭链接</span></span><br><span class="line">&#125;,<span class="number">1000</span>)</span><br><span class="line"><span class="comment">// 1) 先链接数据库</span></span><br><span class="line"><span class="comment">// 2) 创建一个模型 （集合、文件集合）</span></span><br><span class="line"><span class="comment">// 3) 文档</span></span><br></pre></td></tr></table></figure></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> <span class="title class_">UserModel</span> = <span class="built_in">require</span>(<span class="string">&#x27;./model/user&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> <span class="title class_">ArticleModel</span> = <span class="built_in">require</span>(<span class="string">&#x27;./model/article&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> <span class="title class_">CateModel</span> = <span class="built_in">require</span>(<span class="string">&#x27;./model/cate&#x27;</span>);</span><br><span class="line">mongoose.<span class="title function_">connect</span>(<span class="string">`mongodb://127.0.0.1:27017/my`</span>,<span class="keyword">function</span>(<span class="params">err</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(err)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">console</span>.<span class="title function_">log</span>(err);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;数据库链接成功&#x27;</span>)</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">(<span class="keyword">async</span> <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="comment">// let user =await UserModel.create(&#123;username:&#x27;jw&#x27;,password:&#x27;1234567&#x27;,age:10&#125;);</span></span><br><span class="line">    <span class="comment">// let article =  await ArticleModel.create(&#123;title:&#x27;abc&#x27;,content:&#x27;abc&#x27;,user_id:user._id&#125;);</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 6290dbe8d901a81383d62246</span></span><br><span class="line">    <span class="comment">// let article =  await ArticleModel.findById(&#x27;6290dbe8d901a81383d62246&#x27;)</span></span><br><span class="line">    <span class="comment">// let user = await UserModel.findById(article.user_id)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// let article =  await ArticleModel.findById(&#x27;6290dbe8d901a81383d62246&#x27;).populate(&#x27;user_id&#x27;,&#123;username:1&#125;);</span></span><br><span class="line">    <span class="comment">// console.log(article)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 只要复杂的查询全部可以采用聚合查询来解决 aggregate 求平均数 综合， 分组</span></span><br><span class="line">    <span class="comment">// let cate =  await CateModel.create(&#123;title:&#x27;mongo&#x27;&#125;)</span></span><br><span class="line">    <span class="comment">// let article = await ArticleModel.findOneAndUpdate(&#123;_id:&#x27;6290dbe8d901a81383d62246&#x27;&#125;,&#123;cate_id:cate._id&#125;)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// console.log(article)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// let article = await ArticleModel.aggregate([ // 这里的方法属性是原生mongodb</span></span><br><span class="line">    <span class="comment">//     &#123;</span></span><br><span class="line">    <span class="comment">//         $lookup:&#123;</span></span><br><span class="line">    <span class="comment">//             from:&#x27;users&#x27;,</span></span><br><span class="line">    <span class="comment">//             localField:&#x27;user_id&#x27;,</span></span><br><span class="line">    <span class="comment">//             foreignField:&#x27;_id&#x27;, // 我的文章中的user_id 关联的是_id</span></span><br><span class="line">    <span class="comment">//             as:&#x27;user&#x27;</span></span><br><span class="line">    <span class="comment">//         &#125;</span></span><br><span class="line">    <span class="comment">//     &#125;,</span></span><br><span class="line">    <span class="comment">//     &#123;</span></span><br><span class="line">    <span class="comment">//         $lookup:&#123;</span></span><br><span class="line">    <span class="comment">//             from:&#x27;cates&#x27;,</span></span><br><span class="line">    <span class="comment">//             localField:&#x27;cate_id&#x27;,</span></span><br><span class="line">    <span class="comment">//             foreignField:&#x27;_id&#x27;, </span></span><br><span class="line">    <span class="comment">//             as:&#x27;cate&#x27;</span></span><br><span class="line">    <span class="comment">//         &#125;</span></span><br><span class="line">    <span class="comment">//     &#125;,</span></span><br><span class="line">    <span class="comment">//     &#123;</span></span><br><span class="line">    <span class="comment">//         $group:&#123;</span></span><br><span class="line">    <span class="comment">//             _id:&#x27;$username&#x27;,</span></span><br><span class="line">                </span><br><span class="line">    <span class="comment">//         &#125;</span></span><br><span class="line">    <span class="comment">//     &#125;,</span></span><br><span class="line">    <span class="comment">//     &#123;</span></span><br><span class="line">    <span class="comment">//         $match:&#123;</span></span><br><span class="line">    <span class="comment">//             _id: mongoose.Types.ObjectId(&#x27;6290dbe8d901a81383d62246&#x27;)</span></span><br><span class="line">    <span class="comment">//         &#125;</span></span><br><span class="line">    <span class="comment">//     &#125;,</span></span><br><span class="line">    <span class="comment">//     &#123;</span></span><br><span class="line">    <span class="comment">//         $project:&#123;</span></span><br><span class="line">    <span class="comment">//             user:&#123;</span></span><br><span class="line">    <span class="comment">//                 username:1</span></span><br><span class="line">    <span class="comment">//             &#125;,</span></span><br><span class="line">    <span class="comment">//             cate:&#123;</span></span><br><span class="line">    <span class="comment">//                 title:1</span></span><br><span class="line">    <span class="comment">//             &#125;</span></span><br><span class="line">    <span class="comment">//         &#125;</span></span><br><span class="line">    <span class="comment">//     &#125;</span></span><br><span class="line">    <span class="comment">// ]);</span></span><br><span class="line">    <span class="comment">// await UserModel.create([&#123;username:&#x27;jw&#x27;,age:20&#125;,&#123;username:&#x27;xxx&#x27;,age:30&#125;])</span></span><br><span class="line">    <span class="keyword">let</span> user = <span class="keyword">await</span> <span class="title class_">UserModel</span>.<span class="title function_">aggregate</span>([ <span class="comment">// 这里的方法属性是原生mongodb</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="attr">$group</span>:&#123;</span><br><span class="line">                <span class="attr">_id</span>:<span class="string">&#x27;$username&#x27;</span>, <span class="comment">// 根据哪个字段分组</span></span><br><span class="line">                <span class="attr">age</span>:&#123;<span class="attr">$avg</span>:<span class="string">&#x27;$age&#x27;</span>&#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">    ]);</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(user)</span><br><span class="line">    </span><br><span class="line">&#125;)()</span><br><span class="line"></span><br><span class="line"><span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span><br><span class="line">    mongoose.<span class="title function_">disconnect</span>(); <span class="comment">// 关闭链接</span></span><br><span class="line">&#125;,<span class="number">1000</span>)</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// model/article</span></span><br><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">ArticleSchema</span> = mongoose.<span class="title class_">Schema</span>(&#123;</span><br><span class="line">    <span class="attr">title</span>:<span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">content</span>:<span class="title class_">String</span>,</span><br><span class="line">    <span class="attr">birthday</span>:&#123;</span><br><span class="line">        <span class="attr">type</span>:<span class="title class_">Date</span>,</span><br><span class="line">        <span class="attr">default</span>:<span class="title class_">Date</span>.<span class="property">now</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">user_id</span>:&#123;</span><br><span class="line">        <span class="attr">type</span>:mongoose.<span class="property">SchemaTypes</span>.<span class="property">ObjectId</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">cate_id</span>:&#123;</span><br><span class="line">        <span class="attr">type</span>:mongoose.<span class="property">SchemaTypes</span>.<span class="property">ObjectId</span>,</span><br><span class="line">    &#125;,  </span><br><span class="line">    <span class="attr">config</span>:&#123;</span><br><span class="line">        <span class="attr">a</span>:&#123;</span><br><span class="line">            <span class="attr">type</span>:<span class="title class_">String</span>,</span><br><span class="line">            <span class="attr">default</span>:<span class="string">&#x27;a&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="attr">b</span>:&#123;</span><br><span class="line">            <span class="attr">type</span>:<span class="title class_">String</span>,</span><br><span class="line">            <span class="attr">default</span>:<span class="string">&#x27;b&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = mongoose.<span class="title function_">model</span>(<span class="string">&#x27;Article&#x27;</span>,<span class="title class_">ArticleSchema</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// model/cate.js</span></span><br><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果你创建数据的时候 多的是添加不进去的 少的如果不是必填的可以</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">CateSchema</span> = mongoose.<span class="title class_">Schema</span>(&#123; <span class="comment">// 1) 骨架</span></span><br><span class="line">    <span class="attr">title</span>:<span class="title class_">String</span></span><br><span class="line">&#125;,&#123;<span class="attr">timestamps</span>:&#123;</span><br><span class="line">    <span class="attr">createAt</span>:<span class="string">&#x27;create&#x27;</span>, <span class="comment">// 改名字, 默认创建时间和 修改时间</span></span><br><span class="line">    <span class="attr">updateAt</span>:<span class="string">&#x27;update&#x27;</span></span><br><span class="line">&#125;&#125;);</span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = mongoose.<span class="title function_">model</span>(<span class="string">&#x27;Cate&#x27;</span>,<span class="title class_">CateSchema</span>); <span class="comment">//UserModel 就可以来操作数据库</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// model/user.js</span></span><br><span class="line"><span class="keyword">const</span> mongoose = <span class="built_in">require</span>(<span class="string">&#x27;mongoose&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果你创建数据的时候 多的是添加不进去的 少的如果不是必填的可以</span></span><br><span class="line"><span class="keyword">const</span> <span class="title class_">UserSchema</span> = mongoose.<span class="title class_">Schema</span>(&#123; <span class="comment">// 1) 骨架</span></span><br><span class="line">    <span class="attr">username</span>:&#123;</span><br><span class="line">        <span class="attr">type</span>: <span class="title class_">String</span>,</span><br><span class="line">        <span class="comment">// unique:true,</span></span><br><span class="line">        <span class="attr">trim</span>:<span class="literal">true</span>,</span><br><span class="line">        <span class="attr">lowercase</span>:<span class="literal">true</span>,</span><br><span class="line">        <span class="comment">// required:true</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">password</span>:&#123;</span><br><span class="line">        <span class="attr">type</span>:<span class="title class_">String</span>,</span><br><span class="line">        <span class="attr">validate</span>:&#123;</span><br><span class="line">            <span class="title function_">validator</span>(<span class="params">value</span>)&#123; <span class="keyword">return</span> <span class="literal">true</span>&#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">age</span>:&#123;</span><br><span class="line">        <span class="attr">type</span>:<span class="title class_">Number</span>,</span><br><span class="line">        <span class="attr">default</span>:<span class="number">0</span>,</span><br><span class="line">        <span class="attr">min</span>:<span class="number">0</span>,</span><br><span class="line">        <span class="attr">max</span>:<span class="number">100</span></span><br><span class="line">    &#125;,</span><br><span class="line">&#125;,&#123;<span class="attr">timestamps</span>:&#123;</span><br><span class="line">    <span class="attr">createAt</span>:<span class="string">&#x27;create&#x27;</span>, <span class="comment">// 改名字, 默认创建时间和 修改时间</span></span><br><span class="line">    <span class="attr">updateAt</span>:<span class="string">&#x27;update&#x27;</span></span><br><span class="line">&#125;&#125;);</span><br><span class="line"><span class="variable language_">module</span>.<span class="property">exports</span> = mongoose.<span class="title function_">model</span>(<span class="string">&#x27;User&#x27;</span>,<span class="title class_">UserSchema</span>); <span class="comment">//UserModel 就可以来操作数据库</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/wieldy/bailment-blog/2022/03/29/mongo/" data-id="cljmgj1ey000kzc9j9ngcdd13" data-title="mongo" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/bailment-blog/2022/03/29/koa/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          koa
        
      </div>
    </a>
  
  
    <a href="/bailment-blog/2022/03/29/mongo%E6%A6%82%E5%BF%B5/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">mongo概念</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/06/">June 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/04/">April 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/03/">March 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/bailment-blog/2023/07/03/%E5%B7%A5%E4%BD%9C%E6%97%A5%E5%BF%97/">工作日志</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/29/payMent/">payMent</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/20/%E7%BD%91%E7%BA%A2%E9%9D%A2%E8%AF%95%E9%A2%98/">原理题</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/19/%E5%89%8D%E7%AB%AF%E5%A4%A7%E5%B1%8F%E8%87%AA%E9%80%82%E5%BA%94%E6%96%B9%E6%B3%95/">前端大屏自适应方法</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/17/10%E4%B8%AAJavaScript%E5%B8%B8%E8%A7%81%E9%AB%98%E7%BA%A7%E7%9F%A5%E8%AF%86%E7%82%B9/">10个JavaScript常见高级知识点</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/bailment-blog/" class="mobile-nav-link">Home</a>
  
    <a href="/bailment-blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/bailment-blog/js/jquery-3.4.1.min.js"></script>



  
<script src="/bailment-blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/bailment-blog/js/script.js"></script>





  </div>
</body>
</html>